package com.fy.fyspace.controller;

import com.fy.fyspace.common.result.R;
import com.fy.fyspace.service.PerformanceMonitorService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

import static com.fy.fyspace.common.result.R.no;
import static com.fy.fyspace.common.result.R.ok;

/**
 * 性能监控控制器
 */
@Api(tags = "性能监控接口", description = "提供接口性能监控和测试功能")
@Slf4j
@RestController
@RequestMapping("/performance")
public class PerformanceMonitorController {

    @Resource
    private PerformanceMonitorService performanceMonitorService;

    /**
     * 示例接口 - 用于演示性能监控功能
     */
    @ApiOperation(value = "示例接口", notes = "用于演示性能监控功能的示例接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 400, message = "请求失败")
    })
    @GetMapping("/example")
    public R exampleInterface(
            @ApiParam(value = "延迟时间（毫秒）", defaultValue = "100") @RequestParam(defaultValue = "100") Long delay) {
        log.info("示例接口调用 - delay: {}ms", delay);
        try {
            Thread.sleep(delay);
            Map<String, Object> result = new java.util.HashMap<>();
            result.put("message", "示例接口调用成功");
            result.put("delay", delay);
            result.put("timestamp", LocalDateTime.now());
            return ok(result);
        } catch (InterruptedException e) {
            log.error("示例接口执行异常", e);
            return no("接口执行异常: " + e.getMessage());
        }
    }

    /**
     * 模拟慢查询接口
     */
    @ApiOperation(value = "模拟慢查询接口", notes = "用于测试慢查询监控的示例接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 400, message = "请求失败")
    })
    @GetMapping("/slow-example")
    public R slowExampleInterface(
            @ApiParam(value = "延迟时间（毫秒）", defaultValue = "1500") @RequestParam(defaultValue = "1500") Long delay) {
        log.info("慢查询示例接口调用 - delay: {}ms", delay);
        try {
            Thread.sleep(delay);
            Map<String, Object> result = new java.util.HashMap<>();
            result.put("message", "慢查询示例接口调用成功");
            result.put("delay", delay);
            result.put("timestamp", LocalDateTime.now());
            return ok(result);
        } catch (InterruptedException e) {
            log.error("慢查询示例接口执行异常", e);
            return no("接口执行异常: " + e.getMessage());
        }
    }

    /**
     * 模拟异常接口
     */
    @ApiOperation(value = "模拟异常接口", notes = "用于测试异常监控的示例接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 400, message = "请求失败")
    })
    @GetMapping("/exception-example")
    public R exceptionExampleInterface(
            @ApiParam(value = "是否抛出异常", defaultValue = "true") @RequestParam(defaultValue = "true") Boolean throwException) {
        log.info("异常示例接口调用 - throwException: {}", throwException);
        if (throwException) {
            throw new RuntimeException("这是一个模拟的异常");
        }
        Map<String, Object> result = new java.util.HashMap<>();
        result.put("message", "异常示例接口调用成功");
        result.put("throwException", throwException);
        result.put("timestamp", LocalDateTime.now());
        return ok(result);
    }
} 